package com.example.redisjedisspring02.demos;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisZSetCommands;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Set;

// 后续 redis 测试的各种方法都是通过,Controller 提供的 http接口来触发的
@RestController
public class MyController {
      // 通过redisTemplate来操作redis的
      @Autowired
      private StringRedisTemplate redisTemplate;

      @GetMapping("/testString")
      // 将方法返回的对象直接写入Http响应体中
      @ResponseBody
      public String testString(){
          // 清空数据库
          redisTemplate.execute((RedisConnection connection)->{
              connection.flushAll();
              return null;
          });
          // 操作字符串
          // redisTemplate.opsForValue()得到String这个对象
          // 再去操作redis的命令
          redisTemplate.opsForValue().set("key","111");
          redisTemplate.opsForValue().set("key1","222");
          redisTemplate.opsForValue().set("key2","333");

          String value = redisTemplate.opsForValue().get("key");
          System.out.println("value: " + value);

          return "OK";
      }

      @GetMapping("/testList")
      @ResponseBody
      public String testList(){
          // 先删库,清除之前的数据
          // 使用execute可以操作redis原生的删库操作
          redisTemplate.execute((RedisConnection connection)->{
              // 这个execute要求回调方法中必须写return语句,返回这个东西
              // 这个回调返回的对象,就会作为execute本身的返回值 (result)
              connection.flushAll();
              return null;
          });

          redisTemplate.opsForList().leftPush("key","111");
          redisTemplate.opsForList().leftPush("key","222");
          redisTemplate.opsForList().leftPush("key","333");

          String value = redisTemplate.opsForList().rightPop("key");
          System.out.println("value: " + value);
          value = redisTemplate.opsForList().rightPop("key");
          System.out.println("value: " + value);
          value = redisTemplate.opsForList().rightPop("key");
          System.out.println("value: " + value);

          return "OK";
      }

      @GetMapping("/testSet")
      @ResponseBody
      public String testSet(){
          // 清空数据库
          redisTemplate.execute((RedisConnection connection)->{
              connection.flushAll();
              return null;
          });

          redisTemplate.opsForSet().add("key","111","222","333");
          Set<String> values = redisTemplate.opsForSet().members("key");
          System.out.println("values: " + values);

          // 判定set中的某个元素是否存在
          Boolean exists = redisTemplate.opsForSet().isMember("key","111");
          System.out.println("exists: " + exists);

          // 获取集合中的元素个数
          long size = redisTemplate.opsForSet().size("key");
          System.out.println("size: " + size);

          // 删除元素
          redisTemplate.opsForSet().remove("key","111","222");
          values = redisTemplate.opsForSet().members("key");
          System.out.println("values: " + values);

          return "OK";
      }

      @GetMapping("/testHash")
      @ResponseBody
      public String testHash(){
          redisTemplate.execute((RedisConnection connnection)->{
              connnection.flushAll();
              return null;
          });

          redisTemplate.opsForHash().put("key","f1","111");
          redisTemplate.opsForHash().put("key","f2","222");
          redisTemplate.opsForHash().put("key","f3","333");

          String value = (String)redisTemplate.opsForHash().get("key","f1");
          System.out.println("value: " + value);

          // 判定集合中的fidld中f1是否存在
          Boolean flag = redisTemplate.opsForHash().hasKey("key","f1");
          System.out.println("flag: " + flag);

          // 判定集合中有几个元素
          Long size = redisTemplate.opsForHash().size("key");
          System.out.println("size: " + size);

          // 返回成功删除的元素个数
          Long result = redisTemplate.opsForHash().delete("key","f1","f2");
          System.out.println("result: " + result);

          size = redisTemplate.opsForHash().size("key");
          System.out.println("size: " + size);

          return "OK";
      }

      @GetMapping("/testZSet")
      @ResponseBody
      public String testZSet(){
           // 清空数据库
          redisTemplate.execute((RedisConnection connection)->{
              connection.flushAll();
              return null;
          });

          // 插入元素
          redisTemplate.opsForZSet().add("key","zhangsan",30);
          redisTemplate.opsForZSet().add("key","lisi",50);
          redisTemplate.opsForZSet().add("key","wangwu",70);

          // 获取members
          Set<String> values = redisTemplate.opsForZSet().range("key",0,-1);
          System.out.println("values: " + values);

          // 获取 members 和 scores
          Set<ZSetOperations.TypedTuple<String>> memberValues = redisTemplate.opsForZSet().rangeWithScores("key",0,-1);
          System.out.println("memberValues: " + memberValues);

          // 获取对应member的分数
          Double score = redisTemplate.opsForZSet().score("key","zhangsan");
          System.out.println("score: " + score);

          Long size = redisTemplate.opsForZSet().size("key");
          System.out.println("size: " + size);

          redisTemplate.opsForZSet().remove("key","zhangsan");

          size = redisTemplate.opsForZSet().size("key");
          System.out.println("size: " + size);

          Long rank = redisTemplate.opsForZSet().rank("key","lisi");
          System.out.println("rank: " + rank);

          return "OK";
      }
}
